package com.bicycle.rentalsystem.servelt.user;

import com.bicycle.rentalsystem.pojo.user.User;
import com.bicycle.rentalsystem.pojo.user.UserMessage;
import com.bicycle.rentalsystem.service.UserService;
import com.bicycle.rentalsystem.service.impl.user.UserServiceImpl;
import com.bicycle.rentalsystem.service.user.UserMessageService;
import com.bicycle.rentalsystem.service.impl.user.UserMessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = {"/user/user","/user/password"})
public class UserMessageServlet extends HttpServlet {

    private UserMessageService userMessageService = new UserMessageServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String username =(String) session.getAttribute("username");
        UserMessage user = userMessageService.getMessage(username);
        session.setAttribute("user",user);
        resp.sendRedirect("user.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String username =(String) session.getAttribute("username");
        String password =(String) session.getAttribute("password");
        int result = userMessageService.returnMessage(username);
        if (result == 1){
            resp.sendRedirect("user");
        }
    }
}
